home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
answrbok
/
7_10.lha
/
7_10
/
test6.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-08
|
4KB
|
128 lines
* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
* The C++ Answer Book */
* Tony Hansen */
* All rights reserved. */
*ident "@(#)ctrans:demo/task/regtest.C 1.1" */
include <debug.h> /* DELETE */
**************************************************************************
Copyright (c) 1984 AT&T
All Rights Reserved
THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T
The copyright notice above does not evidence any
actual or intended publication of such source code.
****************************************************************************/
include <process.h>
* non-trivial process example:
Make a set of processes which pass an object round between themselves.
Each process but the last creates the next process.
Each process gets an object from the head of one queue, and puts
the object on the tail of another queue.
Each process quits after getting the object MAX_CYCLES times.
Each process uses lots of registers that must be preserved across a switch.
/
onst int NTASKS = 1;
onst int MAX_CYCLES = 5;
oid
irty()
register i, j, k, l, m, z;
i = -1; j = -2; k = -3; l = -4; m = -5; z = -6;
truct pc : process { // derive a class from process
queue_tail *t;
queue_head *h;
char *n;
pc(char*nn, queue_tail*nt, queue_head*nh) // process is not intended to be used directly
: (nn)
{
t = nt; h = nh; n = nn;
}
long main();
;
ong pc::main() // process body serves as
register int i, j, k, l, m, z;
i = *n + 11; j = *n + 12; k = *n + 13;
l = *n + 14; m = *n + 15; z = *n + 16;
printf("new pc(%s)\n",n);
if (*n < 'a'+ NTASKS) {
char* c = new char[2]; c[0] = *n + 1; c[1] = '\0';
queue_tail* qt = new queue_tail;
pc* next = new pc(c, t, qt->head());
next->exec();
t = qt;
} else {
printf("%s: here we go\n", n);
t->put(new process_object);
}
if (this_process() != this) abort();
if (i != *n + 11 || j != *n + 12 || k != *n + 13 ||
l != *n + 14 || m != *n + 15 || z != *n + 16) {
printf("pc: lost regs! i==%d, j==%d, k==%d, l==%d, m==%d, z==%d\n", i, j, k, l, m, z);
printf("vals should be: i: %d, j: %d, k: %d, l: %d, m: %d, z: %d\n", *n + 11, *n + 12, *n + 13, *n + 14, *n + 15, *n + 16);
abort();
}
for (int cycles = 0; cycles < MAX_CYCLES; cycles++) {
process_object* p = h->get();
dirty();
printf("process %s\n",n);
t->put(p);
}
printf("process %s: done.\n", n);
exit(0); // Always end process constructors with resultis.
// Behavior of those using return or running
// off the end of the function is undefined.
// Alternatively, use an infinite loop in
// constructor body.
ain()
register int i, j, k, l, m, z;
queue_head* hh = new queue_head;
queue_tail* t = hh->tail();
queue_head* h;
printf("main\n");
char* n = "a"; // make a one letter process name
i = *n + 1; j = *n + 2; k = *n + 3;
l = *n + 4; m = *n + 5; z = *n + 6;
h = new queue_head;
(new pc(n,t,h))->exec();
printf("main()'s loop\n");
if (i != *n + 1 || j != *n + 2 || k != *n + 3 ||
l != *n + 4 || m != *n + 5 || z != *n + 6) {
printf("main: lost regs! i==%d, j==%d, k==%d, l==%d, m==%d, z==%d\n", i, j, k, l, m, z);
printf("values should be: i: %d, j: %d, k: %d, l: %d, m: %d, z: %d\n", *n + 1, *n + 2, *n + 3, *n + 4, *n + 5, *n + 6);
abort();
}
t = h->tail();
for (int cycles = 0; cycles < MAX_CYCLES; cycles++) {
process_object* p = hh->get();
if (i != *n + 1 || j != *n + 2 || k != *n + 3 ||
l != *n + 4 || m != *n + 5 || z != *n + 6) {
printf("main (after get): lost regs! i==%d, j==%d, k==%d, l==%d, m==%d, z==%d\n", i, j, k, l, m, z);
printf("values should be: i: %d, j: %d, k: %d, l: %d, m: %d, z: %d\n", *n + 1, *n + 2, *n + 3, *n + 4, *n + 5, *n + 6);
abort();
}
printf("main process\n");
t->put(p);
}
printf("main process: done.\n", n);
main_process()->exit(0); // main is a process too; it must also
// end with exit (to allow any
// remaining processes to run, otherwise
// the whole process would exit).
printf("should not get here\n");
return 0;